const used = {
  baseUrl: null,//域名
  //是否为空
  isEmpty: (value) => {
    if (value === undefined || value === null || value === '') {
      return true
    } else {
      return false
    }
  },
  // 深度克隆 - uview拿来的
  deepClone(obj) {
    // 对常见的“非”值，直接返回原来值
    if ([null, undefined, NaN, false].includes(obj)) return obj;
    if (typeof obj !== "object" && typeof obj !== 'function') {
      //原始类型直接返回
      return obj;
    }
    var o = Object.prototype.toString.call(obj) === '[object Array]' ? [] : {};
    for (let i in obj) {
      if (obj.hasOwnProperty(i)) {
        o[i] = typeof obj[i] === "object" ? used.deepClone(obj[i]) : obj[i];
      }
    }
    return o;
  },
  //传时间戳 返回数组形式的日期
  getMyDate(str) {
    str += ''
    str = str.length < 11 ? parseInt(str) * 1000 : parseInt(str);
    var oDate = new Date(str),
      Year = oDate.getFullYear(),
      Month = oDate.getMonth() + 1,
      Day = oDate.getDate(),
      Hour = oDate.getHours(),
      Min = oDate.getMinutes(),
      Sen = oDate.getSeconds(),
      week = oDate.getDay();
    return [Year, addZero(Month), addZero(Day), addZero(Hour), addZero(Min), addZero(Sen), week];

    function addZero(num) {
      if (parseInt(num) < 10) {
        num = '0' + num;
      }
      return num;
    }
  },
  //格式化日期
  formatTimeFun(time, type = 1) {
    let arr = used.getMyDate(time);
    let str = '';
    switch (type) {
      case 1:
        str = arr[0] + '-' + arr[1] + '-' + arr[2];
        break;
      case 2:
        str = arr[0] + '-' + arr[1] + '-' + arr[2] + ' ' + arr[3] + ':' + arr[4] + ':' + arr[5];
        break;
      case 3:
        str = arr[0] + '-' + arr[1] + '-' + arr[2] + ' ' + arr[3] + ':' + arr[4];
        break;
    }
    return str;
  },
  //转换大小
  sizeTostr(size, num = 0) {
    var data = "";
    if (size < 1024) { //如果小于1KB转化成B  
      data = size.toFixed(num) + "B";
    } else if (size < 1024 * 1024) { //如果小于1MB转化成KB  
      data = (size / 1024).toFixed(num) + "KB";
    } else if (size < 1024 * 1024 * 1024) { //如果小于1GB转化成MB  
      data = (size / (1024 * 1024)).toFixed(num) + "MB";
    } else { //其他转化成GB  
      data = (size / (1024 * 1024 * 1024)).toFixed(2) + "GB";
    }
    var sizestr = data + "";
    var len = sizestr.indexOf("\.");
    var dec = sizestr.substr(len + 1, 2);
    if (dec == "00" || dec == "0") { //当小数点后为00时 去掉小数部分  
      return sizestr.substring(0, len) + sizestr.substr(len + 3, 2);
    }
    return sizestr;
  },
  //判断类型
  judgeType: function (str, type) {
    let strType = Object.prototype.toString.call(str); //当前类型
    let endType = ''; //所属类型
    switch (strType) {
      case '[object String]':
        endType = 'String';
        break;
      case '[object Number]':
        endType = 'Number';
        break;
      case '[object Object]':
        endType = 'Object';
        break;
      case '[object Array]':
        endType = 'Array';
        break;
      case '[object Function]':
        endType = 'Function';
        break;
      case '[object Undefined]':
        endType = 'Undefined';
        break;
      case '[object Null]':
        endType = 'Null';
        break;
      case '[object Date]':
        endType = 'Date';
        break;
      case '[object RegExp]':
        endType = 'RegExp';
        break;
      case '[object Boolean]':
        endType = 'Boolean';
        break;
    }
    return endType;
  },
  //获取图片完整地址
  imgUrl(url) {
    if (url.indexOf('http') == -1) {
      return used.baseUrl + url;
    } else {
      return url;
    }
  },
  //转换时间戳为时间 只处理对象和数组类型 不一定完善
  timeTostr(data, arr, type) {
    arr = arr ? arr : ['create_time'];
    type = type ? type : 2;
    //对象 循环
    if (this.judgeType(data) == 'Object') {
      Object.keys(data).forEach(item => {
        //有对于字段 去处理
        if (arr.includes(item)) {
          //字符串和数字 直接转换格式
          if (this.judgeType(data[item]) == 'Number' || this.judgeType(data[item]) == 'String') {
            data[item] = this.formatTimeFun(data[item], type)
          }
        } else if (this.judgeType(data[item]) == 'Object' || this.judgeType(data[item]) == 'Array') {
          this.timeTostr(data[item], arr, type)
        }
      })
    }
    //数组 循环
    if (this.judgeType(data) == 'Array') {
      data.forEach(item => {
        if (this.judgeType(item) == 'Object' || this.judgeType(item) == 'Array') {
          this.timeTostr(item, arr, type)
        }
      })
    }
  }
}

export default used;